package gx.clic.lottery.controller;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.ISelect;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import gx.clic.lottery.dao.mapper.gen.PrizeMapper;
import gx.clic.lottery.dao.util.SessionUtil;
import gx.clic.lottery.entity.db.Prize;
import gx.clic.lottery.entity.db.PrizeExample;

@RestController
//@SpringBootApplication
@RequestMapping(value="/test")
public class TestController {
	@Autowired
	PrizeMapper pm;
	
	@Autowired
	@Qualifier("batchSession")
	SqlSession batchSession;
	
	
	@RequestMapping(value="/tf", method=RequestMethod.GET)
	String testFirst (Model model) {
//		model.addAttribute("name","SpringBootFavicon"); // 并没有什么用
		return "you like my godness. my heart is for you.";
	}
	
	@RequestMapping(value="/gb", method = RequestMethod.GET)
	Prize testGetBean () {
		Prize p = new Prize();
		p.setName("test");
		p.setPrize_type("test_type");
		return p;
	}
	
	@RequestMapping(value="add-data", method= RequestMethod.GET)
	String addData () {
		Prize p = new Prize();
		p.setName("test__");
		p.setPrize_type("test__type");
		p.setRate(-0.1);
		pm.insertSelective(p);
		return "after added";
	}
	
	
	
	@RequestMapping(value="/batch-add", method = RequestMethod.GET)
	String sessionAddTest () {
//		SqlSession session = sessionUtil.getSqlSession(ExecutorType.BATCH);
		PrizeMapper pm = batchSession.getMapper(PrizeMapper.class);
		for (int i = 0; i < 100; i++) {
			Prize p = new Prize();
			p.setName("test__" + i);
			p.setPrize_type("tt_" + i);
			p.setRate(0.0 + i);
			pm.insertSelective(p);
		}
		batchSession.commit();
		return "after added";
	}
	
	
	
	@RequestMapping(value="/get-page", method=RequestMethod.GET)
	Object getPrizePage () {
		Page<Prize> pg = PageHelper.startPage(1, 20).doSelectPage(new ISelect() {
			
			@Override
			public void doSelect() {
				// TODO Auto-generated method stub
				pm.selectByExample(null);
			}
		});
		return pg;
//		return PageHelper.startPage(1, 10).doSelectPageInfo(new ISelect() {
//			
//			@Override
//			public void doSelect() {
//				// TODO Auto-generated method stub
//				pm.selectByExample(new PrizeExample());
//			}
//		});

	}
	
	
	@RequestMapping(value="/ng-post-test", method = RequestMethod.POST)
	void ngPostTest (@RequestBody Map<String, Object> postMap) {
		Set<Entry<String, Object>> entrySet = postMap.entrySet();
		for (Entry<String, Object> entry : entrySet) {
			System.out.println("key: " + entry.getKey() + "    value: " + entry.getValue());
		}
	}
	
}
